package com.qiall.boot.userservice.Impl.organization.activity;

import com.qiall.boot.userservice.data.entity.OrganizationInfo;
import com.qiall.boot.userservice.data.entity.UserInfo;
import com.qiall.boot.userservice.data.mapper.OrganizationInfoMapper;
import com.qiall.boot.userservice.data.mapper.UserMapper;
import com.qiall.user.ability.entity.AddOrganizationDTO;
import com.qiall.user.ability.entity.OrganizationResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 吴金辉
 */
@Component
public class OrgConvert {
    @Autowired
    private OrganizationInfoMapper organizationInfoMapper;
    @Autowired
    private UserMapper userMapper;

    public OrganizationResponse trans(OrganizationInfo organizationInfo) {
        OrganizationResponse organizationResponse = new OrganizationResponse();
        organizationResponse.setId(organizationInfo.getId());
        organizationResponse.setCreateTime(organizationInfo.getCreateTime());
        organizationResponse.setUpdateTime(organizationInfo.getUpdateTime());
        organizationResponse.setOrgName(organizationInfo.getOrgName());
        organizationResponse.setParentId(organizationInfo.getParentId());
        if(organizationInfo.getParentId()!=null){
            OrganizationInfo parent=organizationInfoMapper.select(organizationInfo.getParentId());
            organizationResponse.setParentName(parent.getOrgName());
        }

        organizationResponse.setParentIds(organizationInfo.getParentIds());
        organizationResponse.setOrgDesc(organizationInfo.getOrgDesc());
        organizationResponse.setOrgLeaderId(organizationInfo.getOrgLeaderId());
        UserInfo userInfo = userMapper.select(organizationInfo.getOrgLeaderId());
        organizationResponse.setOrgLeaderName(userInfo.getUserAccount());
        organizationResponse.setSort(organizationInfo.getSort());
        return organizationResponse;
    }

    public OrganizationInfo trans(AddOrganizationDTO addOrganizationDTO) {
        OrganizationInfo organizationInfo = new OrganizationInfo();
        organizationInfo.setOrgName(addOrganizationDTO.getOrgName());
        organizationInfo.setParentId(addOrganizationDTO.getParentId());
        if (addOrganizationDTO.getParentId() != null) {
            OrganizationInfo parent = organizationInfoMapper.select(addOrganizationDTO.getParentId());
            String parentIds = "";
            // build parentIds
            if (StringUtils.isNotBlank(parent.getParentIds())) {
                parentIds = parent.getParentIds() + "," + parent.getId();
            } else {
                parentIds = parent.getId() + "";
            }
            organizationInfo.setParentIds(parentIds);
        }
        organizationInfo.setOrgDesc(addOrganizationDTO.getOrgDesc());
        organizationInfo.setOrgLeaderId(addOrganizationDTO.getOrgLeaderId());
        organizationInfo.setSort(addOrganizationDTO.getSort());
        return organizationInfo;

    }
}
